package com.github.wxiaoqi.security.generator.utils;

import com.github.wxiaoqi.security.generator.entity.ColumnEntity;
import com.github.wxiaoqi.security.generator.entity.TableEntity;
import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.WordUtils;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;

import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.util.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * 代码生成器 工具类
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2016年12月19日 下午11:40:24
 */
public class GeneratorUtils
{

    public static final String INDEX_JS_VM = "index.js.vm";

    public static final String INDEX_VUE_VM = "index.vue.vm";

    public static final String BIZ_JAVA_VM = "biz.java.vm";

    public static final String MAPPER_JAVA_VM = "mapper.java.vm";

    public static final String ENTITY_JAVA_VM = "entity.java.vm";

    public static final String CONTROLLER_JAVA_VM = "controller.java.vm";

    public static final String MAPPER_XML_VM = "mapper.xml.vm";

    public static List<String> getTemplates()
    {
        List<String> templates = new ArrayList<String>();
        templates.add("template/index.js.vm");
        templates.add("template/index.vue.vm");
        templates.add("template/mapper.xml.vm");
        templates.add("template/biz.java.vm");
        templates.add("template/entity.java.vm");
        templates.add("template/mapper.java.vm");
        templates.add("template/controller.java.vm");
        return templates;
    }
    
    /**
     * 生成代码
     */
    public static void generatorCode(Map<String, String> table, List<Map<String, String>> columns, ZipOutputStream zip)
    {
        // 配置信息
        Configuration config = getConfig();
        
        // 表信息
        TableEntity tableEntity = new TableEntity();
        tableEntity.setTableName(table.get("tableName"));
        tableEntity.setComments(table.get("tableComment"));
        // 表名转换成Java类名
        String className = tableToJava(tableEntity.getTableName(), config.getString("tablePrefix"));
        tableEntity.setClassName(className);
        tableEntity.setClassname(StringUtils.uncapitalize(className));
        
        // 列信息
        List<ColumnEntity> columsList = getColumnEntities(columns, config, tableEntity);
        tableEntity.setColumns(columsList);
        
        // 没主键，则第一个字段为主键
        if (tableEntity.getPk() == null)
        {
            tableEntity.setPk(tableEntity.getColumns().get(0));
        }
        
        // 设置velocity资源加载器
        Properties prop = new Properties();
        prop.put("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
        Velocity.init(prop);
        
        // 封装模板数据
        Map<String, Object> map = new HashMap<>(15);
        map.put("tableName", tableEntity.getTableName());
        map.put("comments", tableEntity.getComments());
        map.put("pk", tableEntity.getPk());
        map.put("className", tableEntity.getClassName());
        map.put("classname", tableEntity.getClassname());
        map.put("pathName", tableEntity.getClassname().toLowerCase());
        map.put("columns", tableEntity.getColumns());
        map.put("package", config.getString("package"));
        map.put("author", config.getString("author"));
        map.put("email", config.getString("email"));
        map.put("datetime", DateUtils.format(new Date(), DateUtils.DATE_TIME_PATTERN));
        map.put("moduleName", config.getString("mainModule"));
        map.put("secondModuleName", toLowerCaseFirstOne(className));
        VelocityContext context = new VelocityContext(map);
        
        // 获取模板列表
        List<String> templates = getTemplates();
        for (String template : templates)
        {
            // 渲染模板
            StringWriter sw = new StringWriter();
            Template tpl = Velocity.getTemplate(template, "UTF-8");
            tpl.merge(context, sw);
            
            try
            {
                // 添加到zip
                zip.putNextEntry(new ZipEntry(getFileName(template,
                    tableEntity.getClassName(),
                    config.getString("package"),
                    config.getString("mainModule"))));
                IOUtils.write(sw.toString(), zip, "UTF-8");
                IOUtils.closeQuietly(sw);
                zip.closeEntry();
            }
            catch (IOException e)
            {
                throw new RuntimeException("渲染模板失败，表名：" + tableEntity.getTableName(), e);
            }
        }
    }
    
    private static List<ColumnEntity> getColumnEntities(List<Map<String, String>> columns, Configuration config,
        TableEntity tableEntity)
    {
        List<ColumnEntity> columsList = new ArrayList<>();
        for (Map<String, String> column : columns)
        {
            ColumnEntity columnEntity = new ColumnEntity();
            columnEntity.setColumnName(column.get("columnName"));
            columnEntity.setDataType(column.get("dataType"));
            columnEntity.setComments(column.get("columnComment"));
            columnEntity.setExtra(column.get("extra"));
            
            // 列名转换成Java属性名
            String attrName = columnToJava(columnEntity.getColumnName());
            columnEntity.setAttrName(attrName);
            columnEntity.setAttrname(StringUtils.uncapitalize(attrName));
            
            // 列的数据类型，转换成Java类型
            String attrType = config.getString(columnEntity.getDataType(), "unknowType");
            columnEntity.setAttrType(attrType);
            
            // 是否主键
            if ("PRI".equalsIgnoreCase(column.get("columnKey")) && tableEntity.getPk() == null)
            {
                tableEntity.setPk(columnEntity);
            }
            
            columsList.add(columnEntity);
        }
        return columsList;
    }
    
    /**
     * 列名转换成Java属性名
     */
    public static String columnToJava(String columnName)
    {
        return WordUtils.capitalizeFully(columnName, new char[] {'_'}).replace("_", "");
    }
    
    /**
     * 表名转换成Java类名
     */
    public static String tableToJava(String tableName, String tablePrefix)
    {
        if (StringUtils.isNotBlank(tablePrefix))
        {
            tableName = tableName.replace(tablePrefix, "");
        }
        return columnToJava(tableName);
    }
    
    /**
     * 获取配置信息
     */
    public static Configuration getConfig()
    {
        try
        {
            return new PropertiesConfiguration("generator.properties");
        }
        catch (ConfigurationException e)
        {
            throw new RuntimeException("获取配置文件失败，", e);
        }
    }
    
    /**
     * 获取文件名
     */
    public static String getFileName(String template, String className, String packageName, String moduleName)
    {
        String packagePath = "main" + File.separator + "java" + File.separator;
        String frontPath = "ui" + File.separator;
        if (StringUtils.isNotBlank(packageName))
        {
            packagePath += packageName.replace(".", File.separator) + File.separator;
        }
        
        if (template.contains(INDEX_JS_VM))
        {
            return frontPath + "api" + File.separator + moduleName + File.separator + toLowerCaseFirstOne(className)
                + File.separator + "index.js";
        }
        
        if (template.contains(INDEX_VUE_VM))
        {
            return frontPath + "views" + File.separator + moduleName + File.separator + toLowerCaseFirstOne(className)
                + File.separator + "index.vue";
        }

        if (template.contains(BIZ_JAVA_VM))
        {
            return packagePath + "biz" + File.separator + className + "Biz.java";
        }
        if (template.contains(MAPPER_JAVA_VM))
        {
            return packagePath + "mapper" + File.separator + className + "Mapper.java";
        }
        if (template.contains(ENTITY_JAVA_VM))
        {
            return packagePath + "entity" + File.separator + className + ".java";
        }
        if (template.contains(CONTROLLER_JAVA_VM))
        {
            return packagePath + "rest" + File.separator + className + "Controller.java";
        }
        if (template.contains(MAPPER_XML_VM))
        {
            return "main" + File.separator + "resources" + File.separator + "mapper" + File.separator + className
                + "Mapper.xml";
        }
        
        return null;
    }

    /**
     *  首字母转小写
     * @param s 字符
     * @return
     */
    public static String toLowerCaseFirstOne(String s)
    {
        if (Character.isLowerCase(s.charAt(0)))
        {
            return s;
        }
        else
        {
            return (new StringBuilder()).append(Character.toLowerCase(s.charAt(0))).append(s.substring(1)).toString();
        }
    }
}
